package mediator.example;

/**
 * @Author：LJ
 * @Description：具体中介者
 * @Date: 2017/11/28
 * @Modified By:
 */
public class Mediator extends AbstarctMediator {
    @Override
    public void execute(String str, Object... objects) {
        if (str.equalsIgnoreCase("purchase.buy")) {
            this.buyComputer((Integer) objects[0]);
        }
        if (str.equalsIgnoreCase("sale.sell")) {
            this.sellComputer((Integer) objects[0]);
        }
        if (str.equalsIgnoreCase("sale.offsell")) {
            this.offSell();
        }

        if (str.equalsIgnoreCase("stock.clear")) {
            this.clearStock();
        }
    }

    //采购电脑
    private void buyComputer(int number) {
        int saleStatus = super.sale.getSaleStatus();
        if (saleStatus > 80) {//销售情况良好
            System.out.println("采购IBM电脑:" + number + "台");
        } else {//销售情况不好
            int buyNumber = number / 2;//折半采购
            System.out.println("采购IBM电脑:" + buyNumber + "台");
        }
    }

    //销售电脑
    private void sellComputer(int number) {
        if (super.stock.getStockNumber() < number) {//库存数量不够销售
            super.purchase.buyIBMComputer(number);
        }
        super.stock.decrease(number);
    }

    //折价销售电脑
    private void offSell() {
        System.out.println("折价销售IBM电脑" + stock.getStockNumber() + "台");
    }

    //清仓处理
    private void clearStock() {
        //要求清仓销售
        super.sale.offSale();
        //要求采购人员不要采购
        super.purchase.refuseBuyIBM();
    }
}
